<?php

	class Func extends CI_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('fund_model');
			$this->load->model('kiosk_model');
			$this->load->model('ads_assign_model');
			$this->load->model('trivia_model');
		}
		
		public function test_device_connect(){
			if (!isset($_SERVER['PHP_AUTH_USER'])){
				echo "No auth";
			}
			else{	
				$username 	= $_SERVER['PHP_AUTH_USER'];
				$password 	= $_SERVER['PHP_AUTH_PW'];
				$donate 	= trim($this->input->post('donate'));
				echo $username ."--". $password."--";
				echo $donate;
			}
		}
		
		
		/**
		* get_device_request(): 	get the header http Authentication basic set by kiosk client			
		* 
		* @param
		* @return FALSE or kiosk_id
		* @access public
		*/
		public function get_device_request(){
			if (isset($_SERVER['PHP_AUTH_USER'])){
				$username 	= $_SERVER['PHP_AUTH_USER'];
				$password 	= $_SERVER['PHP_AUTH_PW'];
				return $this->check_api_login($username, $password);
			}
			elseif(isset($_SERVER['HTTP_AUTHENTICATION'])){
				if (strpos(strtolower($_SERVER['HTTP_AUTHENTICATION']),'basic')===0){
					list($username,$password) = explode(':',base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
					return $this->check_api_login($username, $password);
				}
				return FALSE;
			}
			else return FALSE;			
		}
		
		
		/**
		* check_api_login(): 	check the kiosk login from device			
		* 
		* @param String kiosk username, String kiosk password
		* @return FALSE or kiosk_id
		* @access public
		*/		
		public function check_api_login($username, $password){
			$username 	= trim($username);
			$password 	= trim($password);
			
			if(empty($username) || empty($password)) return FALSE;
			
			$result 		= $this->kiosk_model->login($username, $password);
			
			// login success, return the id of kiosk
			if($result){
				$kiosk_id = $result['id'];
				return $kiosk_id;	
			} 	
			else return FALSE;
		}
		
		/**
		* update_kiosh(): 	update the donate from ekokisosk device			
		* 
		* @param 
		* @return 0 or 1
							0: insert failed
							1: insert successfully
		* @access public
		*/
		public function update_kiosh(){						
			$kiosk_id = $this->get_device_request();
			if($kiosk_id!=FALSE){
				$donate = $this->input->post('donate');
				if(empty($donate) || intval($donate)<1)
					echo 0;
				else{
					$result = $this->fund_model->update_donate($kiosk_id, $donate);
					echo $result;
				}
			}
			else echo 0;
		}
		
			
		/**
		* show_total_donate(): 	show the total donate of logged in ekokiosk device			
		* 
		* @param 
		* @return int: total donate
		* @access public
		*/
		public function show_total_donate(){
			$kiosk_id 	= $this->get_device_request();
			if($kiosk_id!=FALSE){
				$arr_data = $this->fund_model->total_donate($kiosk_id);
				$result = $arr_data['total_donate'];
				echo $result;
			}
			else echo 0;
		}
		
		
		/**
		* get_latest_firmware(): 	download the latest firmware			
		* 
		* @param 
		* @return 0 or firmware file
		* @access public
		*/
		public function get_latest_firmware(){
			$kiosk_id 		= $this->get_device_request();
			
			if($kiosk_id!=FALSE){
			
				$folder_upload	= "./download/";			 // folder store the firmware file
				
				// Check the firmware file is in "download" folder
				$data = array();
				if (is_dir($folder_upload)) {
					if ($dh = opendir($folder_upload)) {
						while (($file = readdir($dh)) !== false) {
							if($file!=".." && $file!="." && $file!="apasswords" && $file!=".htaccess"){
								$data['f'] = $file; 
							}
						}
						closedir($dh);
						if(count($data['f']) > 0){
													
							// download file
							
							$filename= $data['f'];	
							$f 			= base_url() . "download/" . $data['f'];
							//redirect($f);
							$handle 	= fopen($f, "r");
							header("Content-Type: application/force-download");
							header('Content-disposition: attachment; filename="'.$filename.'"');
							header('Content-Transfer-Encoding: binary');
							header('Accept-Ranges: bytes');
							header('Cache-Control: private');
							flush();
							readfile($f);
							fclose($handle);
							exit();
						}					
					}
					else echo 0;	
				}
				else echo 0;	
			}
			else echo 0;			
		}

		
		/**
		* get_ads_message(): 	get the text message for the kiosk based on kiosk_id			
		* 
		* @param 
		* @return 0 or firmware file
		* @access public
		*/
		public function get_ads_message(){
			$kiosk_id 		= $this->get_device_request();
			
			if($kiosk_id!=FALSE){
				$result		= $this->ads_assign_model->get_assign_text($kiosk_id);
				if(is_array($result) && count($result)){
					$ad_text	= $result['ad_text'];
					echo $ad_text;
				}
				else echo 0;
			}
			else echo 0;	
		}
		
		
		/**
		* trivia_game(): 	return xml file			
		* 
		* @param 
		* @return 0 or xml file generate from trivia game data
		* @access public
		*/
		public function trivia_game(){
			$kiosk_id 		= $this->get_device_request();
			if($kiosk_id!=FALSE){
				$xml = $this->trivia_model->trivia_game_assign();
				header('Content-type: text/xml');
				echo $xml;
				exit();
			}
			else echo 0;
		}
		
		
		/**
		* get_firmware_name(): 	return firmware file *.bin in download folder		
		* 
		* @param 
		* @return 0 or firmware file name
		* @access public
		*/
		public function get_firmware_name(){
			$kiosk_id 		= $this->get_device_request();
			if($kiosk_id!=FALSE){
				$folder_upload	= "./download/";
				$data = array();
				if (is_dir($folder_upload)) {
					if ($dh = opendir($folder_upload)) {
						while (($file = readdir($dh)) !== false) {
							if($file!=".." && $file!="." && $file!="apasswords" && $file!=".htaccess" && preg_match('/\.bin/', $file)){
								$data['f'] = $file; 
							}
						}
					closedir($dh);
					echo $data['f'];
					}
					else echo 0;
				}
				else echo 0;	
			}
			else echo 0;							
		}
	}
?>